package com.cwd.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.cwd.constant.MessageConstant;
import com.cwd.dao.CheckItemDao;
import com.cwd.entity.PageResult;
import com.cwd.entity.QueryPageBean;
import com.cwd.pojo.CheckItem;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @PackageName: com.cwd.service
 * @ClassName: CheckItemServiceImpl
 * @Author: CWD
 * @Date: 2020/2/29 15:00
 * @Description:
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    //注入dao对象
    @Autowired
    private CheckItemDao checkItemDao;
    //新增检查项
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }
    //检查项分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用mybaits自带的分页助手
        PageHelper.startPage(currentPage,pageSize);
        
        Page<CheckItem> pages = checkItemDao.selectByCondition(queryString);
        long total = pages.getTotal();
        List<CheckItem> result = pages.getResult();
        return new PageResult(total,result);
    }
    //删除检查项
    @Override
    public void delete(Integer id) {
        //删除之前先查看要删除的检查项是否被别的检查组关联
        long count = checkItemDao.findt_grop_item(id);
        if (count > 0) { //被关联
            throw new RuntimeException();
        }else { //未关联
            checkItemDao.delete(id);
        }
    }
    //编辑页面回显
    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemDao.findById(id);
        return checkItem;
    }
    //编辑页面
    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }
    //查询所有检查项
    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItemList = checkItemDao.findAll();
        return checkItemList;
    }
}
